package com.lichongbing.lyoggl.controller;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.lichongbing.lyoggl.model.Flags;
import com.lichongbing.lyoggl.model.Project;
import com.lichongbing.lyoggl.model.Tags;
import com.lichongbing.lyoggl.service.*;
import com.lichongbing.lyoggl.utils.DateUtils;
import com.lichongbing.lyoggl.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2022/4/14 9:14 下午
 * @description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/flags")
public class FlagsController {

    @Autowired
    private FlagsService flagsService;

    @Autowired
    private SubscriptionService subscriptionService;
    @Autowired
    private LyogglUsersService lyogglUsersService;



    @PostMapping("/get")
    public JSONObject getFlags(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            boolean flag = lyogglUsersService.isToken(userid);
            if(flag) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", flagsService.getFlagsByUid(userid));
            return jsonObject;}else {
                jsonObject.put("code",402);
                jsonObject.put("msg","token无效");
                return jsonObject;
            }
        } else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }
    @PostMapping("/getTodo")
    public JSONObject getFlagsByUidMain(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            boolean flag = lyogglUsersService.isToken(userid);
            if(flag) {
                jsonObject.put("code", 200);
                jsonObject.put("msg", "查询成功");
                jsonObject.put("data", flagsService.getFlagsByUidMain(userid));
                return jsonObject;}else {
                jsonObject.put("code",402);
                jsonObject.put("msg","token无效");
                return jsonObject;
            }
        } else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }

    @PostMapping("/getall")
    public JSONObject getFlagsall(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            boolean flag = lyogglUsersService.isToken(userid);
            if(flag) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", flagsService.getFlagsallByUid(userid));
            return jsonObject;}else {
                jsonObject.put("code",402);
                jsonObject.put("msg","token无效");
                return jsonObject;
            }
        } else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }

    /**
     *  添加标签
     * @param request
     * @param flag
     * @return
     */
    @PostMapping("/add")
    public JSONObject addFlags(HttpServletRequest request, @RequestBody JSONObject flag) {
        JSONObject jsonObject = new JSONObject();
        int howlong = Integer.parseInt(flag.getString("howlong"));
        Flags flags = JSONObject.toJavaObject(flag, Flags.class);

        String storeid =  flag.getString("storeid");
        flags.setEstimatedSeconds(Double.valueOf(howlong*3600));
        String token = request.getHeader("token");
        String userid = JwtTokenUtils.getUserid(token);
        String expirationdate = "2022-09-25 01:00:00";
        String subscriptions = subscriptionService.getSubscriptions(userid, storeid);
        if (subscriptions!=null){
            expirationdate = subscriptions;
        }
        String[] split = expirationdate.split("\\.");
        long expirationDate = DateUtils.getStringToLongDate(split[0]);
        long nowtimes = DateTime.now().getTime();
        boolean expiration = JwtTokenUtils.isExpiration(token);
        int flagsRows = flagsService.getFlagsRowsByUid(userid);
        if (expiration == false) {
            if((nowtimes <= expirationDate) || (flagsRows <= 200)){
                jsonObject.put("code", 200);
                jsonObject.put("msg", "添加成功");
                flagsService.addFlags(userid,flags);
                return jsonObject;
            }else {
                jsonObject.put("code", 201);
                jsonObject.put("msg", "服务到期需要订阅升级");
                return jsonObject;
            }
        } else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }

    /**
     *  删除标签
     * @param request
     * @param flagid
     * @return
     */
    @PostMapping("/delete/{flagid}")
    public JSONObject deleteFlags(HttpServletRequest request, @PathVariable String flagid) {
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            boolean flag = lyogglUsersService.isToken(userid);
            if(flag) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "删除成功");
            flagsService.deleteFlags(flagid);
            return jsonObject;}else {
                jsonObject.put("code",402);
                jsonObject.put("msg","token无效");
                return jsonObject;
            }
        } else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }

    /**
     * 修改标签
     * @param request
     * @param flag
     * @return
     */
    @PostMapping("/update")
    public JSONObject updateFlags(HttpServletRequest request, @RequestBody JSONObject flag) {
        JSONObject jsonObject = new JSONObject();
        String fid = flag.getString("id");
        Boolean isCompleted = Boolean.valueOf(flag.getString("isCompleted"));
        String active = "";
        if (isCompleted) {
            active ="1";
        }else {
            active ="0";
        }
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "修改成功");
            flagsService.updateFlags(fid,active);
            return jsonObject;
        } else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }
}
